//获取当前时间
var d = new Date();
var m = d.getMonth() + 1;
var currentYear = d.getFullYear();
var currentTime = d.getFullYear() + "-" + doubleNum(m) + "-" + doubleNum(d.getDate()) + " " + doubleNum(d.getHours())
		+ ":" + doubleNum(d.getMinutes()) + ":" + doubleNum(d.getSeconds());// 当前日期
// 当年1月1日
var startTime = new Date(Date.parse((d.getFullYear() + "-01-01").replace(/-/g, "/")));
var subid = null;
$(function() {
	subid = $("#subid").val();
	$('#f-year').datetimepicker({
		startView : 4,
		minView : 4,
		autoclose : true,
		format : 'yyyy'
	});
	$("#f-year").val(currentYear);
	if (subid) {
		getSpecialData();
//		getBreakerActData();
	}
});
function doubleNum(number) {
	return number < 10 ? "0" + number : number;
}
// 表格
var columns = [ "厂站", "间隔名称","运维单位", "A相", "B相", "C相", "计划传动时间", "传动完成时间"  ];
//var columns = [ "厂站", "间隔名称", "开关名称", "A相", "B相", "C相", "计划时间", "是否在6.30之前应完成" ];
//var columnsAnalyze = [ "厂站", "间隔名称", "开关名称", "A相最后跳闸时间", "B相最后跳闸时间", "C相最后跳闸时间", "计划时间", "是否在6.30之前应完成", "完成时间" ];
// 特维数据
function getSpecialData() {
	$.getJSON("/rest/specialMaintenace/getUnfinishedSpecialStationBySid/" + subid , function(result) {
		//无数据判断
		if(result == {} || result["total"].toString()=="0"){
			$("#noData").show();
			return;
		}else{
			$("#noData").next().show();
			chuanDongFenxi(result, currentTime);
		}	
	});
}
// 动作查询
function getBreakerActData() {
	var year = $("#f-year").val();
	$.getJSON("/rest/specialMaintenace/getBreakerActBySid/" + subid + "/" + year, function(result) {
		var opt = MyTable.getDefaultOptions();
		opt.paging = true;
		opt.searching = true;
		opt.lengthChange = true;
		var tb = new MyTable('special', columns, opt);
		tb.setDataSet(result);
		tb.show();
	});
}
// 传动分析
function chuanDongFenxi(data, currentTime) {
	var date = d.getFullYear() + "-" + m + "-" + d.getDate();
	$("#info .text-yellow").html(date);
	$("#info .text-blue").html(data.total);
	$("#info .text-green").html(data.finishedNum);
	$("#info .text-red").html(data.unfinishedNum);
	// 如果有已完成的
	var optFinished = MyTable.getDefaultOptions();
	optFinished.paging = true;
	optFinished.searching = true;
	optFinished.lengthChange = true;
	var finishedTb = new MyTable('finishedTb', columns, optFinished);
	finishedTb.setDataSet(data.finished);
	finishedTb.show();
	// 如果有未按要求时间完成的
	if (data.demand.length != 0) {
		$(
				"<div class='box box-warning'><div class='box-header'><h3 class='box-title'>未按要求时间完成</h3><div class='box-tools pull-right'>"
						+ "<button type='button' class='btn btn-box-tool' data-widget='collapse'><i class='fa fa-minus'></i></button>"
						+ "</div></div><div class='box-body'>"
						+ "<table id='demand' style='width: 100%;' class='table table-bordered table-hover'>"
						+ "</table></div></div>").appendTo($("#unfinished"));
		var strTime = d.getFullYear() + "-06-30 00:00:00";
//		var demandTime = new Date(Date.parse(strTime.replace(/-/g, "/")));
		var optDemand = MyTable.getDefaultOptions();
		optDemand.paging = true;
		optDemand.searching = true;
		optDemand.lengthChange = true;
		optDemand.columnDefs = [ {
			"targets" : [ 3, 4, 5 ],
//			"createdCell" : function(td, cellData, rowData, row, col) {
//				var time = new Date(Date.parse((cellData).replace(/-/g, "/")));
//				if (time < startTime || time > demandTime) {
//					$(td).css('color', 'red');
//				}
//			}
		} ];
		
		var demandTb = new MyTable('demand', columns, optDemand);
		demandTb.setDataSet(data.demand);
		demandTb.show();
	}
	// 如果有截止目前未完成，但未到要求时间或者没有要求时间
	if (data.plan.length != 0) {
		$(
				"<div class='box box-warning'><div class='box-header'><h3 class='box-title'>截止目前未完成</h3><div class='box-tools pull-right'>"
						+ "<button type='button' class='btn btn-box-tool' data-widget='collapse'><i class='fa fa-minus'></i></button>"
						+ "</div></div><div class='box-body'>"
						+ "<table id='current' style='width: 100%;' class='table table-bordered table-hover'>"
						+ "</table></div></div>").appendTo($("#unfinished"));
		var optCurrent = MyTable.getDefaultOptions();
		optCurrent.paging = true;
		optCurrent.searching = true;
		optCurrent.lengthChange = true;
		optCurrent.columnDefs = [ {
			"targets" : [ 3, 4, 5 ],
//			"createdCell" : function(td, cellData, rowData, row, col) {
//				var time = new Date(Date.parse((cellData).replace(/-/g, "/")));
//				if (time < startTime || time > currentTime) {
//					$(td).css('color', 'red');
//				}
//			}
		} ];
		
		var planTb = new MyTable('current', columns, optCurrent);
		planTb.setDataSet(data.plan);
		planTb.show();
	}
	if (data.demand.length == 0 && data.plan.length == 0) {
		$("<div class='box box-warning'><div class='box-header'><h3 class='box-title'>无未完成数据</h3></div></div>")
				.appendTo($("#unfinished"));
	}
}
